package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.PopularProcess;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ActivityFile;
import com.ruoyi.system.service.IActivityFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 过程管控照片Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/system/file")
public class ActivityFileController extends BaseController
{
    @Autowired
    private IActivityFileService activityFileService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询过程管控照片列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActivityFile activityFile)
    {
        startPage();
//        List<ActivityFile> list = activityFileService.selectActivityFileList(activityFile);
        List<ActivityFile> list = activityFileService.selectActivityFileListNew(activityFile);
        return getDataTable(list);
    }

    /**
     * 导出过程管控照片列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "过程管控照片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivityFile activityFile)
    {
//        List<ActivityFile> list = activityFileService.selectActivityFileList(activityFile);
        List<ActivityFile> list = activityFileService.selectActivityFileListNew(activityFile);
        ExcelUtil<ActivityFile> util = new ExcelUtil<ActivityFile>(ActivityFile.class);
        util.exportExcel(response, list, "过程管控照片数据");
    }

    /**
     * 获取过程管控照片详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityFileService.selectActivityFileById(id));
    }

    /**
     * 新增过程管控照片
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "过程管控照片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityFile activityFile)
    {
        SysDept sysDepts = sysDeptMapper.selectDeptOnName(activityFile.getDeptName());
        if (sysDepts == null) {
            return error("小区名称输入有误，请核对正确后输入");
        }
        activityFile.setDeptId(sysDepts.getDeptId());
        return toAjax(activityFileService.insertActivityFile(activityFile));
    }

    /**
     * 修改过程管控照片
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "过程管控照片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityFile activityFile)
    {
        return toAjax(activityFileService.updateActivityFile(activityFile));
    }

    /**
     * 删除过程管控照片
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "过程管控照片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityFileService.deleteActivityFileByIds(ids));
    }

    @Log(title = "管控照片导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ActivityFile> util = new ExcelUtil<>(ActivityFile.class);
        List<ActivityFile> activityFileList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = activityFileService.importActivityFile(activityFileList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ActivityFile> util = new ExcelUtil<ActivityFile>(ActivityFile.class);
        util.importTemplateExcel(response, "管控照片");
    }
}
